<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品评价管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//商品评价
$return['stars']=array(
		'1'=>'一星',
		'2'=>'二星',
		'3'=>'三星',
		'4'=>'四星',
		'5'=>'五星',
);
$tabs=array(
	array('op'=>'list','title'=>'评价列表'),
	array('op'=>'edit','title'=>'添加评价'),
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_weishop_goods_comments',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'title');
				$item['title']=$item['title']?$item['title']:'未命名商品';
				$item['member']=model('core/core')->member_show($item['mid']);
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>1、移步设置--基础设置--其他设置--可以开启评价审核和自动评价功能！','del_one'=>true,'delall'=>true,'list'=>array(
			//array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'member','title'=>'会员','type'=>'member'),
			array('name'=>'content','title'=>'评价内容','type'=>'string'),
			array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'show','title'=>'是否显示','type'=>'switch','set'=>'set'),
			
		)),
		'search_one'=>array('name'=>'title','title'=>'商品名称','add'=>false),
		
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择评价！','error');
	}
	$show=intval($post['show']);
	$this->pdo_update('goods_comments',array('show'=>$show),array('id'=>$id));
	$message=$show?'已显示':'已隐藏';
	exi($message,'error');
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'content'=>$post['content'],
			'images'=>$post['images']?ijson_encode($post['images']):'',
			'show'=>$post['show'],
			'star'=>$post['star'],
		);
		$id=intval($post['id']);
		if($id){
			$this->pdo_update('goods_comments',$update,array('id'=>$id));
		}else{
			if($post['is_new']==1){
				$member=array(
					'nickname'=>trim($post['name']),
					'uniacid'=>$_SESSION['uniacid'],
					'username'=>'user_'.random(10),
					'from_os'=>get_os(),
					'is_virtual'=>1,
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('core_members',$member);
				$mid=pdo_insertid();
			}
			if($post['createtime']){
				$mins=rand(1,59);
				$createtime=strtotime($post['createtime'])+$mins;
			}
			$update['uniacid']=$_SESSION['uniacid'];
			$update['mid']=$post['mid']?:$mid;
			$update['goodsid']=$post['goodsid'];
			$update['orderid']=0;
			$update['createtime']=$createtime?:TIMESTAMP;
			pdo_insert('jy_weishop_goods_comments',$update);
		}
		exi('操作成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		$item=array(
			'star'=>'5',
		);
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'tabs'=>$tabs,
			'edit'=>array(
				array('name'=>'is_new','title'=>'新会员？','type'=>'switch'),
				array('name'=>'name','title'=>'会员姓名','type'=>'string'),
				array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'core/member.get_member//is_virtual=1','choose_title'=>'选择会员','help'=>'这里只能选择虚拟会员！不会显示真实会员！'),
				array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'选择商品'),				
				array('name'=>'createtime','title'=>'指定评价时间','type'=>'time'),
				array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
				array('name'=>'content','title'=>'评价内容','type'=>'text'),
				array('name'=>'images','title'=>'晒图','type'=>'images'),
				array('name'=>'show','title'=>'是否显示','type'=>'switch'),
			),
		);	
	}else{
		$item=$this->pdo_get('goods_comments',array('id'=>$id));
		$item['goods']=$this->pdo_get('goods',array('id'=>$item['goodsid']),array('title','image'));
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'tabs'=>$tabs,
			'edit'=>array(
				array('type'=>'string','name'=>'goods.title','title'=>'评价商品','read'=>true),
				array('type'=>'image','name'=>'goods.image','title'=>'商品图片','read'=>true),
				array('name'=>'star','title'=>'评价星级','type'=>'radio','options'=>'stars'),
				array('name'=>'content','title'=>'评价内容','type'=>'text'),
				array('name'=>'images','title'=>'晒图','type'=>'images'),
				array('name'=>'show','title'=>'是否显示','type'=>'switch'),
			),
		);	
	}	
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods_comments',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}